package com.yizhuoyan.his.service.pharmacyoperator.ServiceImpl;

import com.yizhuoyan.his.dao.CaseRegistrationRecordDao;
import com.yizhuoyan.his.dao.DocPrescriptionDao;
import com.yizhuoyan.his.dao.DocPrescriptionDetailDao;
import com.yizhuoyan.his.dao.HosDrugsDao;
import com.yizhuoyan.his.entity.CaseRegistrationRecordEntity;
import com.yizhuoyan.his.entity.DocPrescriptionDetailEntity;
import com.yizhuoyan.his.entity.DocPrescriptionEntity;
import com.yizhuoyan.his.entity.HosDrugsEntity;
import com.yizhuoyan.his.qco.CaseRegistrationRecordQco;
import com.yizhuoyan.his.service.pharmacyoperator.DrugSendBackService;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@Service
public class DrugSendBackServiceImpl implements DrugSendBackService {
    @Autowired
    CaseRegistrationRecordDao dao;
    @Autowired
    DocPrescriptionDetailDao dPDao;
    @Autowired
    HosDrugsDao hDao;
    @Autowired
    DocPrescriptionDao Ddao;
    @Override
    public List<CaseRegistrationRecordEntity> queryCaseRegistrationRecord(CaseRegistrationRecordQco qco) throws Exception {
        //清理参数
        CaseRegistrationRecordQco qco1 = new CaseRegistrationRecordQco();
        if(qco!=null){
            String realname=null;
            String no =null;
            if(qco.getRealname()!=null&&qco.getRealname().trim().length()!=0){
                realname=qco.getRealname()+"%";
                qco1.setRealname(realname);
            }
            if (qco.getNo()!=null&&qco.getNo().trim().length()!=0){
                no= qco.getNo()+"%";
                qco1.setNo(no);
            }
        }

        List<CaseRegistrationRecordEntity> caseRegistrationRecordEntities = dao.selectCaseRegistrationRecord(qco1);
        if(caseRegistrationRecordEntities==null){
            throw  new NullPointerException("没有查询到患者");
        }
        return caseRegistrationRecordEntities;
    }

    @Override
    public List<DocPrescriptionDetailEntity> queryDocPrescriptionDetail(  Long id) throws Exception {
        //清理参数
        //不为空
        //执行逻辑
        List<DocPrescriptionDetailEntity> docPrescriptionDetailEntities = dPDao.selectPrescriptionDetailStateById(id);
        if(docPrescriptionDetailEntities==null){
            throw new NullPointerException("患者没有可退的药");
        }
        //组装业务
        return docPrescriptionDetailEntities;
    }

    @Override
    public void modifySendBackDrugCount(Long id, @NonNull int count) throws Exception {
        //清理参数
        //执行逻辑
        Map<String,Object>  map = new HashMap<>();
        map.put("amount",count);
        //组装业务
        dPDao.update(id,map);
    }

    @Override
    public double queryMoney(Long id) throws Exception {
        List<DocPrescriptionDetailEntity> docPrescriptionDetailEntities = dPDao.selectPrescriptionDetailStateById(id);
        if(docPrescriptionDetailEntities==null){
            throw  new NullPointerException("西成药未查询到");
        }

        double money=0;
        for(DocPrescriptionDetailEntity entity :docPrescriptionDetailEntities){
            Long DrugId = entity.getDrugsId();

            HosDrugsEntity hosDrugsEntity = hDao.selectById(DrugId);
            if(hosDrugsEntity!=null){
                Integer unitPrice = hosDrugsEntity.getUnitPrice();
                money=money+unitPrice*entity.getAmount();
            }
        }
        if(money==0){
         log.trace("未查询到相关的钱");
        }
        return money;
    }

    @Override
    public List<DocPrescriptionEntity> queryPrescribeDoctor(Long id) throws Exception {
        List<DocPrescriptionEntity> docPrescriptionEntities = Ddao.selectDoctor(id);
        if(docPrescriptionEntities==null){
            throw new NullPointerException("未查询到医生");
        }
        return docPrescriptionEntities;
    }
}
